<?php

class devicemodel extends CI_Model
{

    public function __construct()
    {
        $this->load->database();
    }

    public function viewind()
    {
        $this->load->helper('url');
        $id = $this->input->post('d_id');
        $query = $this->db->get_where('devices', array('id' => $id));
        return $query->row_array();
    }

    public function addDevice()
    {
        $this->load->helper('url');
        $data = array(
            'id' => $this->input->post('device_id'),
            'serail_id' => $this->input->post('serial_id'),
            'invoice_id' => $this->input->post('invoice_id'),
            'disk' => $this->input->post('disk'),
            'memory' => $this->input->post('memory'),
            'processor' => $this->input->post('processor'),
            'purchase_date' => $this->input->post('purchase_date'),
            'replacement_date' => $this->input->post('replacement_date'),
            'waranty_expiry' => $this->input->post('waranty_expiry'),
            'waranty_years' => $this->input->post('waranty_years'),
            'type' => $this->input->post('type'),
            'status' => $this->input->post('status'),
            'region' => $this->input->post('region'),
            'location' => $this->input->post('location'),
            'comments' => $this->input->post('comments'),
        );
        return $this->db->insert('devices', $data);
    }

    public function getData()
    {
        $this->load->helper('url');
        $id = $this->input->post('id');
        $query = $this->db->get_where('devices', array('id' => $id));
        return $query->row_array();

    }

    public function getreport()
    {
        $this->load->helper('url');
        if (($this->input->post('region') == '') && ($this->input->post('location') == '') && ($this->input->post('purchase_date') == '')) {
            $query = $this->db->get('devices');
        } else {
            $pdate = $this->input->post('purchase_date');
            $region = $this->input->post('region');
            $location = $this->input->post('location');
            $this->db->where('region', $region);
            $this->db->or_where('location', $location);
            $this->db->or_where('purchase_date', $pdate);
            $query = $this->db->get('devices');

        }
        $data['count'] = $query->num_rows();
        $data['stats'] = $query->result_array();
        return $data;

    }

    public function getStats()
    {
        $this->load->helper('url');
        $this->load->helper('date');
        $this->db->where('status', 'Allocated');
        $query = $this->db->get('devices');
        $data['countallocated'] = $query->num_rows();
        $data['statsallocated'] = $query->result_array();
        $this->db->where('status', 'In Stock');
        $query = $this->db->get('devices');
        $data['countunallocated'] = $query->num_rows();
        $data['statsunallocated'] = $query->result_array();
        $this->db->where('type', 'MacBook Pro');
        $query = $this->db->get('devices');
        $data['countmac'] = $query->num_rows();
        $data['statsmac'] = $query->result_array();
        $this->db->where('type', 'Dell');
        $query = $this->db->get('devices');
        $data['countdell'] = $query->num_rows();
        $data['statsdell'] = $query->result_array();
        return $data;
    }

    public function updateData()
    {
        $this->load->helper('url');
        $data = array(
            'id' => $this->input->post('device_id'),
            'serail_id' => $this->input->post('serial_id'),
            'invoice_id' => $this->input->post('invoice_id'),
            'disk' => $this->input->post('disk'),
            'memory' => $this->input->post('memory'),
            'processor' => $this->input->post('processor'),
            'purchase_date' => $this->input->post('purchase_date'),
            'replacement_date' => $this->input->post('replacement_date'),
            'waranty_expiry' => $this->input->post('waranty_expiry'),
            'waranty_years' => $this->input->post('waranty_years'),
            'type' => $this->input->post('type'),
            'status' => $this->input->post('status'),
            'region' => $this->input->post('region'),
            'location' => $this->input->post('location'),
            'comments' => $this->input->post('comments'),
        );
        $this->db->where('id', $data['id']);
        return $this->db->update('devices', $data);
    }

    public function view()
    {
        $this->load->helper('url');
        $pdate = $this->input->post('purchase_date');
        $region = $this->input->post('region');
        $location = $this->input->post('location');
        if (($region == '') && ($location == '') && ($pdate == '')) {
            $query = $this->db->get('devices');
        } else if (($region == 'Select region') && ($location == 'Select location') && ($pdate == '')) {
            $query = $this->db->get('devices');
        } else if (!(($region == 'Select region') || ($location == 'Select location') || ($pdate == ''))) {
            $this->db->where('region', $region);
            $this->db->where('location', $location);
            $this->db->where('purchase_date', $pdate);
            $query = $this->db->get('devices');
        } elseif (($location == 'Select location') && ($pdate == '')) {
            $this->db->where('region', $region);
            $query = $this->db->get('devices');
        } elseif (($region == 'Select region') && ($pdate == '')) {
            $this->db->where('location', $location);
            $query = $this->db->get('devices');
        } elseif (($region == 'Select region') && ($location == 'Select location')) {
            $this->db->where('purchase_date', $pdate);
            $query = $this->db->get('devices');
        } elseif (($region == 'Select region')) {
            $this->db->where('location', $location);
            $this->db->where('purchase_date', $pdate);
            $query = $this->db->get('devices');

        } elseif (($location == 'Select location')) {
            $this->db->where('region', $region);
            $this->db->where('purchase_date', $pdate);
            $query = $this->db->get('devices');

        } elseif (($pdate == '')) {
            $this->db->where('region', $region);
            $this->db->where('location', $location);
            $query = $this->db->get('devices');

        }
        $data['count'] = $query->num_rows();
        $data['devices'] = $query->result_array();
        return $data;

    }
}